package com.green.web.tools;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
/**
 * 修改responsebody注解转换字符串的默认编码
 * @author xsq
 *
 *
 */
@Component
@Lazy(false)
public class StringHttpMessageConverterTools implements BeanPostProcessor{

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if(!(bean instanceof StringHttpMessageConverter)){
			return bean;
		}
		
		final StringHttpMessageConverter stringHttpMessageConverter=(StringHttpMessageConverter) bean;
		final List<MediaType>  supportedMediaTypes=new ArrayList<MediaType>();
		supportedMediaTypes.add(new MediaType("text","plain",Charset.forName("UTF-8")));
		stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}
